package web.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {

    //对于页面跳转，user.do?method=findAll  它会去找servlet名为user.do
    // 的Servlet的service或者doGet() 或者doPost()方法，
    // 如果user.do 这个Servlet没有这些方法，它会去父类去找这个方法，
    // 父类有，则进入父类的这个方法
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        String method = req.getParameter("method");
        if(method!=null){
            //获取到当前对象的类型
            Class<? extends BaseServlet> aClass = this.getClass();
            try {
                //调用方法
                Method declaredMethod = aClass.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
                System.out.println(declaredMethod.getName());
                declaredMethod.invoke(this,req,resp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
